import csv
import json
import time
import threading

import pytest

from business.login import Login
from pom.base import BaseDriver, logger
from selenium import webdriver

from common.pytest_getdata import getData
log = logger
class TestCookie():
    def setup_method(self):
        # 以静默模式打开浏览器 不展示页面 后台运行
        option = webdriver.ChromeOptions()
        option.add_argument('headless')
        self.driver = webdriver.Chrome(chrome_options=option)
        # self.driver = webdriver.Chrome()
        BaseDriver(self.driver).get_url()
        self.l = Login(self.driver)

    @pytest.mark.parametrize('name,passwd', getData(r"D:\pythoncode\trainning\common\user.json"))
    def test_login(self, name, passwd):
        csv_writer_lock = threading.Lock()
        self.l.system_login(name, passwd)
        time.sleep(1)
        cookie = self.driver.get_cookies()
        with csv_writer_lock:
            with open(r"C:\Users\JYJ\Desktop\用户cookie.csv", 'a+', newline = '') as file:
                writer = csv.writer(file)
                sid,x = None,None
                for i in cookie:
                    if i['name'] == 'TrainingPlatform-sid':
                        sid = i['value']
                    elif i['name'] == 'XSRF-TOKEN':
                        x = i['value']
                    else:
                        continue
                writer.writerow(['TrainingPlatform-sid='+sid,x])

                log.info(name)
                log.info(sid + x)

    def teardown_method(self):
        BaseDriver(self.driver).quit_webdriver()

if __name__ == '__main__':
    pytest.main()